﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using TrelloNet;

namespace SprintTrelloAutomater
{
    public class TrelloHelper : INotifyPropertyChanged
    {
        TrelloNet.Trello _trello;

        #region Sprints Property
        private List<string> _sprints;
        public List<string> Sprints
        {
            get { return _sprints; }
            set
            {
                _sprints = value;
                OnPropertyChanged("Sprints");
            }
        }
        #endregion

        #region TrelloAuthKey Property
        private string _authKey;
        public string TrelloAuthKey
        {
            get
            {
                return _authKey;
            }
            set
            {
                _authKey = value;
                OnPropertyChanged("TrelloAuthKey");
                Sprints = GetAllBoardsForUser(BoardFilter.Open);
            }
        }
        #endregion

        public Uri AuthorizationURL { get; set; }

        public DelegateCommand SaveAuthTokenCmd { get; set; }

        private string RoydanOrgID;
        private NameValueCollection _settings = ConfigurationManager.AppSettings;

        private bool CanSaveAuthToken(object o)
        {
            return true;
        }

        private void SaveTrelloKey(object o)
        {
            //_settings["TrelloKey"] = TrelloAuthKey;
            _settings.Set("TrelloKey", TrelloAuthKey);
        }

        public TrelloHelper()
        {
            SaveAuthTokenCmd = new DelegateCommand(SaveTrelloKey, CanSaveAuthToken);

            string trelloKey = _settings["TrelloKey"];
            if (string.IsNullOrEmpty(trelloKey))
            {
                MessageBox.Show("TrelloKey is missing in the app configuration");
                return;
            }

            //string trelloAPIKey = settings["TrelloAPIKey"];
            //if (!string.IsNullOrEmpty(trelloAPIKey))
            //    TrelloAuthKey = trelloAPIKey;
            //else
            //{
            //    TrelloAuthKey = "";
            //    return;
            //}

            _trello = new Trello(trelloKey);
            //webBrowser.Navigate(trello.GetAuthorizationUrl(versionString, Scope.ReadWrite));
            string versionString = "Roydan Sprint Board Automater v" + FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion;
            AuthorizationURL = _trello.GetAuthorizationUrl(versionString, Scope.ReadWrite);

            // TODO: Convert string to IOrganizationId
            //if (!String.IsNullOrEmpty(RoydanOrgID))
            //Sprints = this.GetAllBoardsforOrg((IOrganizationId)RoydanOrgID);
            //else

            // TODO: Need to authorize the user before calling this
            Sprints = GetAllBoardsForUser(BoardFilter.Open);
        }

        public void SaveAuthKey(string key, string val)
        {
            _settings[key] = val;
        }

        public List<string> GetAllBoardsforOrg(IOrganizationId org)
        {
            return _trello.Boards.ForOrganization(org, BoardFilter.Open).Select<Board, string>(b => b.Name).ToList<string>();
        }

        public List<string> GetAllBoardsForUser(BoardFilter filter)
        {
            return new List<string>();

            // TODO: exception is being thrown here

            var boards = _trello.Boards.ForMe(filter);

            var x = boards.Select<Board, string>(b => b.Name);

            var li = x.ToList<string>();

            List<string> foo = _trello.Boards.ForMe(filter).Select<Board, string>(b => b.Name).ToList<string>();
            return foo;
        }

        public List<string> GetAllLists(Board board)
        {
            return _trello.Lists.ForBoard(board, ListFilter.Open).Select<List, string>(l => l.Name).ToList<string>();
        }

        public Dictionary<Color, string> GetAllCategories(Board board)
        {
            return _trello.Boards.WithId(board.Id).LabelNames;
        }

        public List<string> GetAllMembers(IOrganizationId org)
        {
            // TODO: Use avatar here?
            return _trello.Members.ForOrganization(org, TrelloNet.MemberFilter.All).Select<Member, string>(m => m.FullName).ToList<string>();
        }

        #region INotifyPropertyChanged Implementation
        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
        #endregion
    }
}
